<template>
	<view class="login-container">

		<view class="wechatapp">
			<view class="header">
				<open-data class="" type="userAvatarUrl"></open-data>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			 
			<button class="btn-normal" @click="getCode">授权登录</button>
			 
		</view>
		<view class="no-login-btn">
			<button class="btn-normal" @click="onNotLogin">暂不登录</button>
		</view>
	</view>
</template>

<script>
	import {
		gotopage
	} from '@/common/gotopage.js';
	export default {
		data() {
			return {
				background: '',
				listData: [],
				invitation_id: 0
			}
		},
		onLoad(e) {
			this.invitation_id = uni.getStorageSync('invitation_id') ? uni.getStorageSync('invitation_id') : 0;
		// this.gotoPage('/pages/index/index','reLaunch');
		// this.gotoPage(url, 'reLaunch');
		},
		methods: {
			onNotLogin: function() {
				this.gotoPage('/pages/index2/index');
			},
			 getCode() {
			      let appid = "wx992eaa00687a597a"; //个人开发者appid
			      let redirect = encodeURIComponent(window.location.href); //重定向回来的地址
			      let wx_code = this.getUrlParam("code"); // 截取url中的code
			      //判断有没有code
			      if (!wx_code) {
			        //获取code的地址。获取成功重定向后地址栏中将会带有code，判断没有code的话，就跳转到微信官方链接上获取，获取成功后会再重定向回来，注意url是需要使用encodeURIComponent处理一下编码的
			        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+redirect+'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
			      } else {
			     	// 获取到了code
					console.log(wx_code)
			        this.getOpenId(wx_code); //把code传给后台获取用户信息
			      }
			    },
			    //getUrlParam方法就是使用正则截取地址栏里的code，有兴趣可以研究一下，没兴趣直接拿来用
			    getUrlParam: function (name) {
			      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			      var r = window.location.search.substr(1).match(reg);
			      if (r != null) return unescape(r[2]);
			      return null;
			    },
			     getOpenId(code) {
					 console.log(11111111)
					 let self = this;
					//这里调用后端给的接口吧code传过去就好了
					self._get('/index/index6', {code:code}, function(res) {
						 console.log(222222222)
								console.log(res)
							  if (res.code==1){
								  self.gotoPage('/pages/index2/index?openid='+res.data.openid);
							  }
							  
						 });
				}


		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}
</style>
